
//
//  GCDViewController.m
//  RunTime
//
//  Created by renxlin on 16/8/12.
//  Copyright © 2016年 rxl. All rights reserved.
//

#import "GCDViewController.h"

@interface GCDViewController()
@property (strong, nonatomic) NSOperationQueue *queue;
@property (strong, nonatomic) dispatch_queue_t disQueue;
@end

@implementation GCDViewController

- (id)init {
    self = [super init];
    if (self) {
        self.queue = [[NSOperationQueue alloc] init];
//        [self.queue setMaxConcurrentOperationCount:4];
        self.disQueue = dispatch_queue_create("sersQueue", DISPATCH_QUEUE_CONCURRENT);
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    NSInvocationOperation *op1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(op1) object:nil];
    [self.queue addOperation:op1];
    NSInvocationOperation *op2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(op2) object:nil];
    [self.queue addOperation:op2];
    
//    op1.completionBlock = ^(){
////        NSLog(@"operation end");
//    };
    
//    [op1 cancel];
//    [self.queue cancelAllOperations];
//    
//    dispatch_async(self.disQueue, ^{
//        NSLog(@"111operation end %lu",(unsigned long)self.queue.operationCount);
//        sleep(2);
//    });
//    
//    dispatch_async(self.disQueue, ^{
//        NSLog(@"222operation end %lu",(unsigned long)self.queue.operationCount);
//        sleep(1);
//    });
    
//    dispatch_suspend(self.disQueue);

    
}

- (void)op1 {
    NSLog(@"start 1");
    sleep(2);
    NSLog(@"operation 1");
    
}

- (void)op2 {
    NSLog(@"start2");
    sleep(1);
    NSLog(@"operation 2");
    
}

@end
